package org.jeecg.modules.bicycle.locus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.bicycle.locus.entity.BikeVehicleElectLocus;
import org.jeecg.modules.bicycle.locus.mapper.BikeVehicleElectLocusMapper;
import org.jeecg.modules.bicycle.locus.service.IBikeVehicleElectLocusService;
import org.jeecg.modules.bicycle.locus.vo.BikeVehicleElectLocusRes;
import org.jeecg.modules.bicycle.locus.vo.LocusReturnRes;
import org.jeecg.modules.bicycle.locus.vo.VehicleElectLocusRes;
import org.jeecg.modules.bicycle.locus.vo.VehicleLocusRes;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.station.entity.BikeStation;
import org.jeecg.modules.param.station.mapper.BikeStationMapper;
import org.jeecg.modules.utils.CaculateDistanceUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @Description: 电动车运动轨迹
 * @Author: 月月鸟
 * @Date:   2021-07-13
 * @Version: V1.0
 */
@Service
public class BikeVehicleElectLocusServiceImpl extends ServiceImpl<BikeVehicleElectLocusMapper, BikeVehicleElectLocus> implements IBikeVehicleElectLocusService {
    @Resource
    private BikeVehicleElectLocusMapper bikeVehicleElectLocusMapper;
    @Resource
    private BikeStationMapper bikeStationMapper;


    @Override
    public Result<?> getLocus(VehicleElectLocusRes vehicleElectLocusRes) {
        //输出对象
        LocusReturnRes locusReturnRes = new LocusReturnRes();
        //判断时间是否为空
        if (vehicleElectLocusRes.getEndTime()==null){
            vehicleElectLocusRes.setEndTime(new Date());
        }
        //查询时间段之间的轨迹点
        List<BikeVehicleElectLocusRes> vehicleLocus=bikeVehicleElectLocusMapper.getVehicleLocus(vehicleElectLocusRes);
        //升序排序
        vehicleLocus.sort(new Comparator<BikeVehicleElectLocusRes>() {
            @Override
            public int compare(BikeVehicleElectLocusRes o1, BikeVehicleElectLocusRes o2) {
                return o1.getCreateTime().compareTo(o2.getCreateTime());
            }
        });
        ////判断步长
        //if (vehicleElectLocusRes.getArticle()>0){
        //    int count = 0 ;
        //    String dell =null;
        //    //获取需要过滤的下标
        //    for (int i = 0 ; i < vehicleLocus.size();i++){
        //        //不判断开头结尾
        //        if (i!=0&&i!=vehicleLocus.size()-1){
        //            //如果count不等于步长
        //            if (vehicleElectLocusRes.getArticle()!=count){
        //                //count+1
        //                count++;
        //                if (dell==null){
        //                    dell=i+"";
        //                }else {
        //                    dell=dell+","+i;
        //                }
        //            }else {
        //                count = 0 ;
        //            }
        //        }
        //    }
        //    String[] split = dell.split(",");
        //    //删除下标
        //    for (int i =split.length-1; i>=0;i-- ){
        //        vehicleLocus.remove(Integer.parseInt(split[i]));
        //    }
        //}
        //转为数组
        ArrayList<ArrayList> locusArray = this.toArray(vehicleLocus);
        //保存轨迹下标
        locusReturnRes.setLocus(locusArray);
        //double格式
        DecimalFormat format = new DecimalFormat("#.00");
        double s = 0 ;
        String dell =null;
        for (int i = 0 ; i < vehicleLocus.size() ; i++){
            //当前数据
            BikeVehicleElectLocusRes newLocus = vehicleLocus.get(i);
            if (i != 0 ){
                //上一个数据
                BikeVehicleElectLocusRes lowLocus = vehicleLocus.get(i-1);
                //获取距离
                GlobalCoordinates newGo = new GlobalCoordinates(Double.parseDouble(newLocus.getLongitude()), Double.parseDouble(newLocus.getLatitude()));
                GlobalCoordinates lowGo = new GlobalCoordinates(Double.parseDouble(lowLocus.getLongitude()), Double.parseDouble(lowLocus.getLatitude()));
                double distanceMeter = CaculateDistanceUtils.getDistanceMeter(lowGo, newGo, Ellipsoid.WGS84);
                //判断是否删除距离少于BikeConstant.locus_speed_filter米的定位
                if (StringUtils.isNotEmpty(vehicleElectLocusRes.getSpeedFilter())){
                    if (vehicleElectLocusRes.getSpeedFilter().equals(BikeConstant.y_n_yes)){
                        if (distanceMeter<BikeConstant.locus_speed_filter){
                            if (dell==null){
                                dell=i+"";
                            }else {
                                dell=dell+","+i;
                            }
                        }
                    }
                }
                s = s + distanceMeter;
                //获取当前里程
                newLocus.setDistance(format.format(s));
                //获取附近站点和距离
                lowLocus.setStation(this.getStation(lowLocus));
            }
            //获取附近站点和距离
            newLocus.setStation(this.getStation(newLocus));
        }
        //删除距离低于BikeConstant.locus_speed_filter米的下标
        if (StringUtils.isNotEmpty(vehicleElectLocusRes.getSpeedFilter())){
            if (vehicleElectLocusRes.getSpeedFilter().equals(BikeConstant.y_n_yes)){
                if (dell!=null){
                    String[] split = dell.split(",");
                    //删除下标
                    for (int i =split.length-1; i>=0;i-- ){
                        vehicleLocus.remove(Integer.parseInt(split[i]));
                    }
                }
            }
        }
        //降序
        vehicleLocus.sort(new Comparator<BikeVehicleElectLocusRes>() {
            @Override
            public int compare(BikeVehicleElectLocusRes o1, BikeVehicleElectLocusRes o2) {
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });
        //保存轨迹列表
        locusReturnRes.setVehicleElectLocusRes(vehicleLocus);
        return Result.OK(locusReturnRes);
    }

    private ArrayList<ArrayList> toArray(List<BikeVehicleElectLocusRes> locus) {
        ArrayList<ArrayList> list = new ArrayList<>();
        for (int i = 0 ; i < locus.size() ; i++){
            BikeVehicleElectLocusRes locusRes = locus.get(i);
            ArrayList<String> list1 = new ArrayList<>();
            list1.add(0,locusRes.getLongitude());
            list1.add(1,locusRes.getLatitude());
            list.add(i,list1);
        }
        return list;
    }


    private String getStation(BikeVehicleElectLocusRes lowLocus) {
        List<BikeStation> bikeStations = bikeStationMapper.selectAllList();

        for (int i = 0 ; i < bikeStations.size() ; i++){
            BikeStation bikeStation = bikeStations.get(i);
            GlobalCoordinates newGo = new GlobalCoordinates(Double.parseDouble(bikeStation.getLongitude()), Double.parseDouble(bikeStation.getLatitude()));
            GlobalCoordinates lowGo = new GlobalCoordinates(Double.parseDouble(lowLocus.getLongitude()), Double.parseDouble(lowLocus.getLatitude()));
            bikeStation.setDistance(CaculateDistanceUtils.getDistanceMeter(lowGo, newGo, Ellipsoid.WGS84));
        }
        bikeStations.sort(new Comparator<BikeStation>() {
            @Override
            public int compare(BikeStation o1, BikeStation o2) {
                BigDecimal newDistance = BigDecimal.valueOf(o1.getDistance());
                BigDecimal lowDistance = BigDecimal.valueOf(o2.getDistance());
                return newDistance.compareTo(lowDistance);
            }
        });

        DecimalFormat format = new DecimalFormat("#.00");
        BikeStation bikeStation = bikeStations.get(0);
        return "距离"+bikeStation.getName()+format.format(bikeStation.getDistance())+"米";
    }



}

